import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author zhu
 * @date 2024-04-03
 */
public class MainTest {
    public static void main(String[] args) throws Exception {
        Student stu = new Student("zs","男",new String[]{"火影","原神"});

        Class<? extends Student> clazz = stu.getClass();
        Field sexField = clazz.getDeclaredField("sex");
        Field hobbiesField = clazz.getDeclaredField("hobbies");
        sexField.setAccessible(true);
        hobbiesField.setAccessible(true);
        String strSex = (String) sexField.get(stu);
        String[] strHobbies = (String[]) hobbiesField.get(stu);
        if (clazz.isAnnotationPresent(SetInfo.class)){
            SetInfo annotation = clazz.getAnnotation(SetInfo.class);
            String[] gender = annotation.gender();
            String[] hobbies = annotation.hobby();
            Set<String> setGender = new HashSet<>(Arrays.asList(gender));
            Set<String> setHobbies = new HashSet<>(Arrays.asList(hobbies));
            Set<String> set2 = new HashSet<>(Arrays.asList(strHobbies));

            if(!setGender.contains(strSex)){
                throw new RuntimeException("性别非法，性别只能是：" + Arrays.toString(gender));
            }

            if(!setHobbies.containsAll(set2)){
                throw new RuntimeException("爱好非法，爱好只能是：" + Arrays.toString(hobbies));
            }
        }

        System.out.println("姓名：" + stu.getName() + "，性别：" + stu.getSex());
        System.out.println("爱好：" + Arrays.toString(stu.getHobbies()));
    }
}
